package com.xuhuan.admin.filter;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import java.io.IOException;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.RequestFacade;

/**
 * @author: huan.xu
 * @create: 2024/6/12 22:16
 * @description: 日志打印
 **/
@Slf4j
@WebFilter(filterName = "logFilter",urlPatterns = "/*")
public class LogFilter implements Filter {

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
      FilterChain filterChain) throws IOException, ServletException {
    log.info("[{}]访问[{}]请求：{}", LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN)
        ,((RequestFacade) servletRequest).getMethod()
        ,((RequestFacade) servletRequest).getRequestURI());
    filterChain.doFilter(servletRequest,servletResponse);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    Filter.super.init(filterConfig);
  }

  @Override
  public void destroy() {
    Filter.super.destroy();
  }
}
